﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using STOCK.DAL;
using System.IO;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using STOCK.Model;

namespace STOCK
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            if (cmbCat.SelectedItem == null)
            {
                MessageBox.Show("请选择函数类别");
                return;
            }
            Guid CatId = (Guid)cmbCat.SelectedValue;
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*.*)|*.*";
            if (fileDialog.ShowDialog() != null)
            {
                string file = fileDialog.FileName;
                string content = File.ReadAllText(file,Encoding.Default);
                Insert(content,CatId);
                MessageBox.Show("OK");
            }

           
        }

        private void Insert(string content, Guid  CatId)
        {
            string[] p = Regex.Split(content, "\r\n");//分行

            StringBuilder sb = new StringBuilder();

            foreach (string str in p)
            {
                if (str.Length <= 0)
                {
                    continue;
                }
                if (Convert.ToInt32(str[0]) >= 48 & Convert.ToInt32(str[0]) <= 57)
                {
                    sb.Append("|" + str + "\r\n");

                }
                else
                {
                    sb.Append(str + "\r\n");
                }
            }

            string[] str0 = sb.ToString().Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);//分开各个函数
            foreach (string str1 in str0)
            {
                T_Function function = new T_Function();
                string[] str2 = str1.Split(new string[] { "\r\n" }, 2, StringSplitOptions.RemoveEmptyEntries);//1  ALLBIDVOL(N,M) 委托买入总量与后面分开
                string[] str3 = Regex.Split(str2[0], "\t");//1  与 ALLBIDVOL(N,M)委托买入总量 分开
                string funs = str3[1];
                for (int i = 0; i < funs.Length; i++)
                {
                    int asc = Convert.ToInt32(funs[i]);
                    if (asc == 40 || asc == 41 || asc == 44 || asc == 46 || (asc <= 90 & asc >= 65) || (asc <= 57 & asc >= 48) || (asc <= 122 & asc >= 97))
                    {
                    }
                    else
                    {
                        //ALLBIDVOL(N,M) 与 委托买入总量 分开
                       function.Fun = funs.Substring(0, i);
                       function.Name = funs.Substring(i).Trim();
                        break;
                    }
                }
                function.Discription= DeleteEnter(str2[1]);
                new T_FunctionDAL().InsertByCatId(function, CatId);
            }

        }

        private string DeleteEnter(string discription)
        {
            string[] lines=discription.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string sb ="";
            foreach (string line in lines)
            {
                sb = sb + line.Trim();
                
            }
            return sb;

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cmbCat.ItemsSource = new T_CategoryDAL().ListAll();

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuild_Click(object sender, RoutedEventArgs e)
        {
            T_Category[] cates =(T_Category[]) cmbCat.ItemsSource;
            StreamWriter sw = new StreamWriter("FUCTION.txt", true);
            foreach (T_Category cate in cates)
            {
                T_Function[] funs = new T_FunctionDAL().GetByCatId(cate.Id);
                //StreamWriter sw = new StreamWriter(cate.Name + ".txt", true);
                
                foreach (T_Function fun in funs)
                {                  
                    sw.WriteLine("/// <summary>");
                    sw.WriteLine("///"+fun.Name+fun.Discription);
                    sw.WriteLine("/// </summary>");
                    List<string> param=FindParam( fun.Fun);
                    if (param.Count > 0)
                    {
                        for (int i = 0; i < param.Count; i++)
                        {
                            sw.WriteLine("/// <param name=\"" + param[i] + "\"></param>");
                        }
                        sw.WriteLine("public static int " + AddInt(fun.Fun, param));
                        sw.WriteLine("{");
                        sw.WriteLine("return 1;");
                        sw.WriteLine("}");
                    }
                    else
                    {
                        sw.WriteLine("public static int " + fun.Fun + "{set;get;}");
                    }

                }
                //sw.Flush();

            }
            sw.Flush();
            MessageBox.Show("OK!");

        }

        private string AddInt(string function, List<string> list)
        {
                int pos = function.IndexOf('(');
                string funA=function.Substring(0,pos+1);
                if (function.Contains("..."))
                {
                    return funA+"int[] M)";
                }
                for (int i = 0; i < list.Count; i++)
                {
                    if (i == (list.Count - 1))
                    {
                        funA= funA + "int " + list[i]+")";
                    }
                    else
                    {
                        funA = funA + "int " + list[i] + ",";
                    }
                    
                    
                }
                return funA;

        }
        private List<string> FindParam(string function)
        {
            List<string> list = new List<string>();
            if(function.Contains('('))
            {
                for (int i = 0; i < function.Length; i++)
                {
                    char ch = function[i];
                    if (ch == ')')
                    {
                        break;
                    }
                    char ch1 = function[i + 1];
                    if ((ch == '(' || ch == ',') & ch1 != '.')
                    {
                        list.Add(function.Substring(i + 1, 1));
                    }
                }
            }
            return list;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
        }
    }
}
